﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Globalization;

namespace EasyLibrary.AR.Classes
{
   public class RotaMarca:ActiveRecord
    {
       public int IDRotaMarca { get; set; }
       public string Morada { get; set; }
       public string Label { get; set; }
       public double Latitude { get; set; }
       public double Longitude { get; set; }
       public int Ordem { get; set; }
       public DateTime DataInsercao { get; set; }
       public bool Activo { get; set; }
       public Rota Rota { get; set; }


      public RotaMarca()
      {
        
          this.Rota=new Rota();
      }

      public RotaMarca(int id,string adress,string lbl,float lat,float lng,int ord,DateTime date,bool active,Rota r)
      {
          this.IDRotaMarca = id;
          this.Morada = adress;
          this.Label = lbl;
          this.Latitude = lat;
          this.Longitude = lng;
          this.Ordem = ord;
          this.DataInsercao = date;
          this.Activo = active;
          this.Rota=r;

      }
       public RotaMarca(DataRow row)
       {
         /*  rm.Morada = (string)r["Morada"];
           rm.Label = (string)r["Label"];
           rm.Latitude = (float)r["Latitude"];
           rm.Longitude = (float)r["Longitude"];
           rm.Ordem = (int)r["Ordem"];
           rm.DataInsercao = (DateTime)r["DataInsercao"];
           rm.Activo = (bool)r["Activo"];
           rm.Rota.Nome = (string)r["Nome"];
           rm.IDRotaMarca = (int)r["IDRotaMarca"];*/

           this.Rota.IDRota = Convert.ToInt32(row["IDRota"].ToString());
           this.Morada = (string)row["Nome"];
           this.Label = (string)row["Label"];
           this.Latitude = (double)row["Latitude"];
           this.Longitude = (double)row["Longitude"];
           this.Ordem = (int)row["Ordem"];
           this.DataInsercao = (DateTime)row["DataInsercao"];
           this.Activo = (bool)row["Destino"];
           this.Rota.Nome = (string)row["Nome"];
           this.IDRotaMarca = Convert.ToInt32(row["IDRotaMarca"].ToString());

       }
      
       public IList<RotaMarca> getMarcasByRota(int idRota)
       {
           var ret = new List<RotaMarca>();

           try
           {
               DataSet ds = ExecuteQuery(GetConnection(false), "SELECT Rotas.IDRota, RotasMarca.Morada, RotasMarca.Label, RotasMarca.Latitude, RotasMarca.Longitude, RotasMarca.Ordem, RotasMarca.DataInsercao, RotasMarca.Activo, Rotas.Nome, RotasMarca.IDRotaMarca FROM Rotas INNER JOIN RotasMarca ON Rotas.IDRota = RotasMarca.RefIDRota WHERE (Rotas.IDRota = "+idRota+")");



               foreach (DataRow r in ds.Tables[0].Rows)
               {
                   RotaMarca rm = new RotaMarca();
                   string bigint = (r["IDRota"]).ToString();
                   rm.Rota.IDRota = Convert.ToInt32(bigint);
                   rm.Morada = (string)r["Morada"];
                   rm.Label = (string)r["Label"];
                   rm.Latitude = (double)r["Latitude"];
                   rm.Longitude = (double)r["Longitude"];
                   rm.Ordem = (int)r["Ordem"];
                   rm.DataInsercao = (DateTime)r["DataInsercao"];
                   rm.Activo = (bool)r["Activo"];
                   rm.Rota.Nome = (string)r["Nome"];
                   bigint = (r["IDRotaMarca"]).ToString();
                   rm.IDRotaMarca = Convert.ToInt32(bigint);
                
                   ret.Add(rm);
               }

               return ret;
           }
           catch (Exception ex)
           {
               throw new ApplicationException("Erro BD", ex);
           }

           

           return ret;
       } 

       public override void Save()
       {
           throw new NotImplementedException();
       }

       public RotaMarca getMarcaById(int IdRotaMarca)
       {
           try
           {

               DataSet ds = executeCommandOnDataSet(string.Format(CultureInfo.InvariantCulture, "Select latitude,longitude from rotasMarca where IDRotaMarca={0}", IdRotaMarca));

               //IList ret = new ArrayList();
               RotaMarca rm = new RotaMarca();
               foreach (DataRow r in ds.Tables[0].Rows)
               {
                   rm.Latitude=(double)r["Latitude"];
                   rm.Longitude = (double) r["Longitude"];
               }

               return rm;
           }
           catch (Exception ex)
           {
               throw new ApplicationException("Erro BD", ex);
           }
       }
    }
}
